// reference promiseaplus SPEC.
// https://promisesaplus.com/
// http://www.ituring.com.cn/article/66566 http://malcolmyu.github.io/malnote/2015/06/12/Promises-A-Plus/

// 1.1
// “promise” is an object or function with a then method whose behavior conforms to this specification.
// promise 是一个拥有 then 方法的对象或函数，其行为符合本规范；
// @example
// @example.函数
// function promise() {}
// promise.then = function () {}
// @example.对象
// const promise = {then() {}}

// 1.2
// “thenable” is an object or function that defines a then method.
// 是一个定义了 then 方法的对象或函数，文中译作“拥有 then 方法”；
// @example 如上

// 1.3
// “value” is any legal JavaScript value (including undefined, a thenable, or a promise).
// 指任何 JavaScript 的合法值（包括 `undefined` , `thenable` 和 `promise`）；

// 1.4
// “exception” is a value that is thrown using the throw statement.
// 是使用 throw 语句抛出的一个值。

// 1.5
// “reason” is a value that indicates why a promise was rejected.
// 表示一个 promise 的拒绝原因。

// 2.
/**
 * 状态
 */
const STATES = {
  PENDING: 0,
  FULFILLED: 1,
  REJECTED: 2
};
// 1.2 “thenable” is an object or function that defines a then method.
/**
 * 是否为thenable对象
 * @param {Object|Function} obj thenable 对象
 */
const isThenable = obj => {
  return (typeof obj === "object" || isFunc(obj)) && obj && "then" in obj;
};
/**
 * 是否为方法
 * @param {Function} fn 方法
 */
const isFunc = fn => {
  return typeof fn === "function";
};

class PromiseES6V0 {
  onFulfilledCallbacks = [];
  onRejectedCallbacks = [];
  eventualValue = undefined;

  constructor(fn) {
    // 2.1 Promise States
    this.state = this.STATES.PENDING;
  }
  then(onFulfilled, onRejected) {
    // 2.2.1.1 If onFulfilled is not a function, it must be ignored.
    if (this.isFunc(onFulfilled)) {
      this.callbacks.push(onFulfilled);
    }

    // 2.2.1.2 If onRejected is not a function, it must be ignored.
    if (this.isFunc(onRejected)) {
      this.callbacks.push;
    }
  }
  catch(onRejected) {}
  transition(state) {}
  static resolve(eventualValue) {}
  static reject(reason) {}
  static all(...arg) {}
  static race(...arg) {}
}
